﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Partner.Surround.Authorization;
using Partner.Surround.Controllers;
using Partner.Surround.Users;
using Partner.Surround.Web.Models.Users;
using Partner.Surround.Users.Dto;
using Partner.Surround.Notifications;
using Partner.Surround.Web.Models.Common;
using Partner.Surround.Notifications.Dto;

namespace Partner.Surround.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Users)]
    public class UsersController : SurroundControllerBase
    {
        private readonly IUserAppService _userAppService;
        private readonly INotificationAppService _notificationAppService;

        public UsersController(IUserAppService userAppService,
            INotificationAppService notificationAppService)
        {
            _userAppService = userAppService;
            _notificationAppService = notificationAppService;
        }

        public async Task<ActionResult> Index()
        {
            var users = (await _userAppService.GetAll(new PagedUserResultRequestDto {MaxResultCount = int.MaxValue})).Items; // Paging not implemented yet
            var roles = (await _userAppService.GetRoles()).Items;
            var model = new UserListViewModel
            {
                Users = users,
                Roles = roles
            };
            return View(model);
        }

        public async Task<ActionResult> EditUserModal(long userId)
        {
            var user = await _userAppService.Get(new EntityDto<long>(userId));
            var roles = (await _userAppService.GetRoles()).Items;
            var model = new EditUserModalViewModel
            {
                User = user,
                Roles = roles
            };
            return View("_EditUserModal", model);
        }

        #region 消息通知设定
        /// <summary>
        /// 通知设置
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> NotificationSetting()
        {
            var notificationSettings = await _notificationAppService.GetNotificationSettings();
            return View(notificationSettings);
        }

        /// <summary>
        /// 更新通知订阅
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateNotificationSetting([FromBody]UpdateNotificationSettingsInput input)
        {
            await _notificationAppService.UpdateNotificationSettings(input);
            return Json(new ResponseParamViewModel("通知设置已更新"));
        }
        #endregion
    }
}
